package com.bigShen.mybatis.official;

import com.bigShen.mybatis.official.dao.InstrumentInfoMapper;
import com.bigShen.mybatis.official.dao.UserInfoMapper;
import com.bigShen.mybatis.official.entity.InstrumentInfo;
import com.bigShen.mybatis.official.entity.InstrumentInfoKey;
import com.bigShen.mybatis.official.entity.UserInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.util.List;

/**
 * @author : lei.shen
 * @date : 2018/11/21
 */
public class MybatisMain {

    /**
     * 测试 typeHandler 枚举转换;
     * 测试 plugin 打印sql语句以及参数;
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

//        SqlSession defaultSqlSession = null;
//        SqlSession secondSqlSession = null;
//        try {
//
//            String resource = "mybatis-config.xml";
//            SqlSessionFactory defaultSqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource));
//
//            defaultSqlSession = defaultSqlSessionFactory.openSession();
//            UserInfoMapper defaultUserInfoMapper = defaultSqlSession.getMapper(UserInfoMapper.class);
////            List<UserInfo> defaultUserInfo = defaultUserInfoMapper.selectUserByAccount("T100001");
////            System.out.println(defaultUserInfo);
//
////            SqlSessionFactory secondSqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource), "zxDev");
////
////            secondSqlSession = secondSqlSessionFactory.openSession();
////            UserInfoMapper secondUserInfoMapper = secondSqlSession.getMapper(UserInfoMapper.class);
////            List<UserInfo> secondUserinfo = secondUserInfoMapper.selectUserByAccount("ht-15");
////            System.out.println(secondUserinfo);
//
//            List<UserInfo> userInfo = defaultUserInfoMapper.selectUserLike("T", "A", 123);
//            System.out.println(userInfo);
//
//            InstrumentInfoMapper instrumentInfoMapper = defaultSqlSession.getMapper(InstrumentInfoMapper.class);
//            System.out.println(instrumentInfoMapper.selectByPrimaryKey("111709220", "SPT_BD", "X_CNBD"));
//
//        } finally {
//    //            sqlSession.commit();
//            if (null != defaultSqlSession) {
//                defaultSqlSession.close();
//            }
//            if (null != secondSqlSession) {
//                secondSqlSession.close();
//            }
//        }

        testInsert();

    }


    public static void testInsert() {

        String resource = "mybatis-config.xml";
        SqlSession sqlSession = null;
        try {

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource));
            sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE, false);

            InstrumentInfoMapper mapper = sqlSession.getMapper(InstrumentInfoMapper.class);
            InstrumentInfo instrumentInfo = new InstrumentInfo();
            mapper.insert(instrumentInfo);
            sqlSession.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }


    }


}
